<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller
{
	private $_login_validation_rules = array(
			array('field' => 'uname',    'label' => 'Username',    'rules' => 'trim|required|xss_clean'),
			array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required|xss_clean|alpha_dash')
	);

	
    private $data = array();

	function __construct()
	{
		parent::__construct();
		$this->load->library('form_validation');
	}

	public function index()
	{
		$this->login();
	}

	public function login()
	{
		$this->form_validation->set_rules($this->_login_validation_rules);

        $error = '';

		if ($this->form_validation->run())
		{
            $this->config->load('admin');
            $users = $this->config->item('admin_users');

            $uname  = $this->input->post('uname');
            $passwd = $this->input->post('password');

            $keys = array_keys($users);

            if(!in_array($uname, $keys))
            {
                $error = "Invalid Username.";
            }
            else if(strcmp($users[$uname], $passwd) !== 0)
            {
                $error = "Invalid Password.";
            }
            else
            {
                $this->session->set_userdata('logged_in', TRUE);
                $this->session->set_userdata('uname', $uname);
                redirect('');
            }


		}

		$this->data['error'] = $error;

		$this->layout->view('login', $this->data);
	}
	
	public function logout()
	{
		
		$this->session->sess_destroy();
	
		redirect('login');
	}

	
	 
}

?>
